package com.hqyj.lls.service.impl;

import java.lang.String;
import com.hqyj.lls.entity.MyFile;
import com.hqyj.lls.mapper.MyFileMapper;
import com.hqyj.lls.service.MyFileService;
import com.hqyj.lls.utils.ResultUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Service
public class MyFileServiceImpl implements MyFileService {

    @Autowired
    private MyFileMapper myFileMapper;

    ///查询出所有数据
    @Override
    public ResultUtils fileList() {
        return new  ResultUtils(200,"",myFileMapper.selectAll());
    }

    @Override
    public ResultUtils insert(MyFile myFile) {
        myFileMapper.insert(myFile);
        return new ResultUtils(200,"添加成功",null);
    }

    //文档的上传
    @Override
    public ResultUtils upload(MultipartFile multipartFile, HttpServletRequest request) {
        try {
            //1.获取文件上传目录的路径
            String filePath = request.getSession().getServletContext().getRealPath("upload");
            System.out.println("上传文件路径"+filePath);
            //2.获取上传的文件名
            String fileName = multipartFile.getOriginalFilename();
            System.out.println("上传的文件名" + fileName);
            //3.创建file对象
            File f = new File(filePath + "/" + fileName);
            //4.判断当前文件上传的目录是否存在
            if (!f.isDirectory()){
                //创建目录
                f.mkdirs();
            }
            //5.上传文件
            multipartFile.transferTo(f);
            //6.创建map封装记录文件名及文件路径
            HashMap<String, Object> map = new HashMap<>();
            //localhost: 8080/upload/a.txt
            map.put("servicePath","/upload/"+fileName);
            map.put("fileName",fileName);
            return new ResultUtils(200,"上传成功",map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResultUtils(500,"上传失败",null);
    }
    //文档的下载
    @Override
    public ResponseEntity<byte[]> downLoad(String fileName, HttpServletRequest request) throws IOException {
        //1.获取文档上传的路径
        String filePath = request.getSession().getServletContext().getRealPath("upload");
        //2.创建文件对象
        File file = new File(filePath + "/" + fileName);
        //3.设置httpHeaders，使浏览器响应下载
        HttpHeaders headers = new HttpHeaders();
        //4.设置响应方式
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //5.设置响应头
        /*
        headerName:
            content-Type:响应的内容格式
            content-Disposition:处理方式
                    - inline:直接在页面展示
                    - attchment:以附件的形式下载
         乱码处理：
         */
        headers.set("Content-Disposition","attachment;filename="
                + new String(fileName.getBytes("utf-8"),"ISO8859-1")
                +"");
        //将文件以二进制形式写回
        return new  ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
